package sistemadeproyectos;

import java.sql.*;
import javax.swing.JOptionPane;

public class RegistrarEquipo extends javax.swing.JDialog {

    private Usuario usuarioActivo = new Usuario();
    
    public void setUsuario(Usuario elUsuario){
        this.usuarioActivo = elUsuario;
    }
    
    /** Creates new form RegistrarEquipo */
    public RegistrarEquipo(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        this.getRootPane().setDefaultButton(jbtnAceptar);
        jtxtEquipo.requestFocus();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jtxtEquipo = new javax.swing.JTextField();
        jpwfClave = new javax.swing.JPasswordField();
        jpwfConfirmar = new javax.swing.JPasswordField();
        jbtnAceptar = new javax.swing.JButton();
        jbtnCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sistemadeproyectos.SistemadeproyectosApp.class).getContext().getResourceMap(RegistrarEquipo.class);
        setTitle(resourceMap.getString("Form.title")); // NOI18N
        setName("Form"); // NOI18N

        jLabel1.setFont(resourceMap.getFont("jLabel1.font")); // NOI18N
        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setFont(resourceMap.getFont("jLabel2.font")); // NOI18N
        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setFont(resourceMap.getFont("jLabel3.font")); // NOI18N
        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setFont(resourceMap.getFont("jLabel4.font")); // NOI18N
        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jtxtEquipo.setText(resourceMap.getString("jtxtEquipo.text")); // NOI18N
        jtxtEquipo.setName("jtxtEquipo"); // NOI18N

        jpwfClave.setText(resourceMap.getString("jpwfClave.text")); // NOI18N
        jpwfClave.setName("jpwfClave"); // NOI18N

        jpwfConfirmar.setText(resourceMap.getString("jpwfConfirmar.text")); // NOI18N
        jpwfConfirmar.setName("jpwfConfirmar"); // NOI18N

        jbtnAceptar.setText(resourceMap.getString("jbtnAceptar.text")); // NOI18N
        jbtnAceptar.setName("jbtnAceptar"); // NOI18N
        jbtnAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnAceptarActionPerformed(evt);
            }
        });

        jbtnCancelar.setText(resourceMap.getString("jbtnCancelar.text")); // NOI18N
        jbtnCancelar.setName("jbtnCancelar"); // NOI18N
        jbtnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbtnCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(105, 105, 105)
                        .addComponent(jLabel1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel4, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jLabel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                        .addGap(23, 23, 23)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jpwfConfirmar)
                            .addComponent(jpwfClave)
                            .addComponent(jtxtEquipo, javax.swing.GroupLayout.DEFAULT_SIZE, 206, Short.MAX_VALUE))))
                .addContainerGap(48, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(91, Short.MAX_VALUE)
                .addComponent(jbtnAceptar)
                .addGap(79, 79, 79)
                .addComponent(jbtnCancelar)
                .addGap(84, 84, 84))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(27, 27, 27)
                .addComponent(jLabel1)
                .addGap(33, 33, 33)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jtxtEquipo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jpwfClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jpwfConfirmar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbtnAceptar)
                    .addComponent(jbtnCancelar))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jbtnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_jbtnCancelarActionPerformed

    private void jbtnAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbtnAceptarActionPerformed
        // TODO add your handling code here:
        if(jtxtEquipo.getText().equals("")){
            JOptionPane.showMessageDialog(null, "El nombre del equipo no puede ser vacio", "Datos incompletos", JOptionPane.ERROR_MESSAGE);
            jtxtEquipo.requestFocus();
        }
        else if(jpwfClave.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Clave vacia", "Datos incompletos", JOptionPane.ERROR_MESSAGE);
            jpwfClave.requestFocus();
        }
        else if(jpwfConfirmar.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Clave confirmacion vacia", "Datos incompletos", JOptionPane.ERROR_MESSAGE);
            jpwfConfirmar.requestFocus();
        }
        else {
            try{
                BDAdmin motor = new BDAdmin("SELECT * FROM EQUIPO WHERE ID_EQUIPO = '" + jtxtEquipo.getText() + "'");
                ResultSet rs = motor.consulta();
                if(rs.next() == true){
                    JOptionPane.showMessageDialog(this, "Ya está registrado ese equipo en el sistema", 
                        "Equipo duplicado", JOptionPane.ERROR_MESSAGE);
                }
                else{
                    if(jpwfClave.getText().equals(jpwfConfirmar.getText())){
                        motor.setQuery("INSERT INTO EQUIPO VALUES('" + jtxtEquipo.getText() + "','" + jpwfClave.getText() + "')");
                        motor.actualizar();
                        JOptionPane.showMessageDialog(this, "El equipo " + jtxtEquipo.getText() + " ha sido creado correctamente!", 
                        "Registro exitoso", JOptionPane.INFORMATION_MESSAGE);
                        ((UnirseEquipo)this.getParent()).listarEquipos();
                        this.dispose();
                    }
                    else{
                        JOptionPane.showMessageDialog(this, "Las claves no coinciden!", 
                            "Diferencia de claves", JOptionPane.ERROR_MESSAGE);
                    }
                }
                motor.cerrar();
            }
            catch(SQLException sqle){
                sqle.printStackTrace();
            }
        }
    }//GEN-LAST:event_jbtnAceptarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                RegistrarEquipo dialog = new RegistrarEquipo(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JButton jbtnAceptar;
    private javax.swing.JButton jbtnCancelar;
    private javax.swing.JPasswordField jpwfClave;
    private javax.swing.JPasswordField jpwfConfirmar;
    private javax.swing.JTextField jtxtEquipo;
    // End of variables declaration//GEN-END:variables
}